package com.shuaitao.newsystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuaitao.newsystem.bean.Comments;
import com.shuaitao.newsystem.service.CommentsService;
import com.shuaitao.newsystem.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentsService commentsService;


//    @RequestMapping("commentInit")
//    public String commentInit(@RequestParam("nid") Integer nid,HttpSession session) {
//        List<Comments> detailComments = commentsService.getDetailComments(nid, pageNum, pageSize);
//        session.setAttribute("comments",detailComments);
//        return "/newspages/comment";
//    }
    @RequestMapping("/getDetailComment")
    public String getDetailComments(@RequestParam("nid") Integer nid, HttpSession session,
                                    @RequestParam("pageNum") Integer pageNum) {
        IPage<Comments> comments = commentsService.getDetailComments(nid,pageNum);
        List<Comments> records = comments.getRecords();
        session.setAttribute("comments",records);
        session.setAttribute("commentsPage",comments);
        long pages = comments.getPages();
        session.setAttribute("commentPages",pages);
        return "/newspages/comment";
    }

    @RequestMapping("/getAdminDetailComment")
    public String getAdminDetailComments(@RequestParam("nid") Integer nid,
                                         HttpSession session,
                                         @RequestParam("pageNum") Integer pageNum) {
        IPage<Comments> comments = commentsService.getDetailComments(nid, pageNum);
        List<Comments> records = comments.getRecords();
        session.setAttribute("comments",records);
        session.setAttribute("commentsPage",comments);
        long pages = comments.getPages();
        session.setAttribute("commentPages",pages);
        return "/newspages/admin_comment";
    }

    @RequestMapping("/delComments")
    @ResponseBody
    public R delComment(@RequestParam("cid") Integer cid) {
        R r = commentsService.delComment(cid);
        return r;
    }

    @RequestMapping("/addComment")
    @ResponseBody
    public R addComment(@RequestParam("cauthor") String cauthor, @RequestParam("cnid") Integer cnid,
                        @RequestParam("cip") String cip,
                        @NotBlank @RequestParam("ccontent") String content) {
        Comments comments = new Comments();
        comments.setCip(cip);
        comments.setCnid(cnid);
        comments.setCauthor(cauthor);
        comments.setCcontent(content);
        comments.setCdate(new Date());
        boolean save = commentsService.save(comments);
        if (save){
//            List<Comments> detailComments = commentsService.getDetailComments(cnid);
//            session.setAttribute("comments",detailComments);
            return R.ok();
        }
        return R.error(101,"评论失败");
    }
}
